package com.college.heyuhua.controller;

import com.college.heyuhua.model.shop.Category;
import com.college.heyuhua.model.shop.Item;
import com.college.heyuhua.model.shop.Orderinfo;
import com.college.heyuhua.model.shop.Take_delivery_of_goods;
import com.college.heyuhua.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵东坡 on 2019/2/6.
 * Time:10:42
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
public class ShopController {

    @Autowired
    private IShopService iShopService;

    /*商城主页*/
    @RequestMapping("/shophome1")
    public String shopHome(ModelMap map) {
        List<Item>  itemsList = null;
        itemsList = iShopService.getItemList();
        map.put("itemsList",itemsList);

        List<Category> categoryList = null;
        categoryList = iShopService.getCategoryList();
        map.put("categoryList",categoryList);

        return "shophome";
    }


    @RequestMapping("/cart")
    public String cart(ModelMap map) {
        /*判断是否登录了*/
        long uid = 1l;
        List<Item> itemList = iShopService.getCart(uid);

        int priceTotal = 0;
        for(int i = 0; i < itemList.size();i++) {
            int sum = Integer.parseInt(itemList.get(i).getPrice());
            int cartcount = iShopService.getCartInfo(itemList.get(i).getIid());
            priceTotal += (sum*cartcount);
            itemList.get(i).setCartcount(cartcount+"");
        }
        map.put("itemList",itemList);
        map.put("priceTotal",priceTotal);
        return "cart";
    }

    @RequestMapping("/shopitem/cid/{cid}")
    public String shopitem(@PathVariable("cid") String cid,ModelMap map) {
        List<Item> itemList = null;
        itemList = iShopService.getItemListByCid(Integer.parseInt(cid));
        map.put("itemList",itemList);
        return "shop";
    }

    @RequestMapping("/singleproduct/iid/{iid}")
    public String singleproduct(@PathVariable("iid") String iid,ModelMap map) {

        Item item = iShopService.getItemByIid(iid);
        map.put("item",item);
        return "singleproduct";
    }

    @RequestMapping("/addcart1/iid/{iid}")
    public String addcart1(@PathVariable("iid") String iid, ModelMap map){

        Integer id = Integer.parseInt(iid);
        long uid = 1l;
        boolean flag = iShopService.addcart(id,1,uid);
        if(flag==false){
            return "404";
        }
        return "redirect:/shophome1";
    }


    @RequestMapping("/addform")
    public void formsunmit(@RequestParam("countss") String countss,@RequestParam("iid") String iid,HttpServletResponse response) {

        long uid = 1l;
        boolean flag = iShopService.addcartbycounts(Integer.parseInt(iid),1,uid,countss);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(flag == false) {
            writer.print("修改失败");
            writer.close();
            writer.flush();
        }
        writer.print("修改成功");
        writer.close();
        writer.flush();
    }


    @RequestMapping("/addcart/iid/{iid}/cid/{cid}")
    public String addcart(@PathVariable("iid") String iid,
                          @PathVariable("cid") String cid, ModelMap map){

        Integer id = Integer.parseInt(iid);
        long uid = 1l;
        boolean flag = iShopService.addcart(id,1,uid);
        if(flag==false){
            map.put("message","添加失败,可能是库存数量不够");
        }else{
            map.put("message","添加成功");
        }
        List<Item> itemList = null;
        itemList = iShopService.getItemListByCid(Integer.parseInt(cid));
        map.put("itemList",itemList);
        return "shop";
    }


    @RequestMapping("/updates/iid/{iid}/counts/{counts}")
    public void updates(@PathVariable("iid") String iid, @PathVariable("counts") String counts, HttpServletResponse response) {
        boolean flag = false;


        flag = iShopService.updates(iid,counts);

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(flag == false) {
            writer.print("修改失败");
            writer.close();
            writer.flush();
        }
        writer.print("修改成功");
        writer.close();
        writer.flush();

    }
    @RequestMapping("/deletes/iid/{iid}")
    public String deletes(@PathVariable("iid") String iid) {
        boolean flag = false;
        flag = iShopService.deletes(iid);
        return "redirect:/cart";
    }

    @RequestMapping("/shoptheall")
    public String shoptheall(ModelMap map) {
        long uid = 1l;
        boolean flag = iShopService.shoptheall(uid);

        boolean flg = iShopService.deletesCart(uid);

        return "redirect:/person";
    }

    @RequestMapping("/person")
    public String person(ModelMap map) {
        long uid = 1l;
        List<Orderinfo> orderinfoList = iShopService.getOrderByuid(uid);
        List<Item> itemList = new ArrayList<>();
        for(int i = 0 ; i< orderinfoList.size(); i++) {
            String itemstr = orderinfoList.get(i).getItemid();
            if(!StringUtils.isEmpty(itemstr)) {
                itemstr = itemstr.substring(1,itemstr.length()-1);
            }
            String[] stritems = itemstr.split(",");
            for(int t = 0; t < stritems.length; t++) {
                Item items = iShopService.getItemByIid(stritems[t]);
                itemList.add(items);
            }
        }




        map.put("itemList",itemList.subList(0,3));
        Take_delivery_of_goods take_delivery_of_goods = iShopService.getTakeofgoods(uid);
        map.put("take_delivery_of_goods",take_delivery_of_goods);
        return "person";
    }


    @RequestMapping("/updateinfo")
    public String updateinfo(@RequestParam("address") String address,
                             @RequestParam("tel") String tel) {
        long uid = 1l;
        boolean flag = iShopService.updateinfo(address,tel,uid);
        return "redirect:/person";
    }
}
